package com.yj.cloudnote.controller;

import com.yj.cloudnote.model.Page;
import com.yj.cloudnote.model.Share;
import com.yj.cloudnote.service.FilesService;
import com.yj.cloudnote.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: jy
 * @Description:
 * @Date: 16:29 2018/6/11
 */
@Controller
@RequestMapping(value = "/share")
public class ShareController {
    @Autowired
    private ShareService shareService;
    @Autowired
    private FilesService filesService;

    /**
     * @Author: jy
     * @Description:
     * @Date: 16:29 2018/6/11
     */
    @RequestMapping(value = "/getShareUrl")
    @ResponseBody
    public Map<String,Object> createShareUrl(@RequestBody Map<String,Object> params) {
        String username = (String)params.get("username");
        int file_id = (int)params.get("file_id");
        String file_name = (String)params.get("file_name");
        Map<String,Object> res = new HashMap<String,Object>();
        Share share = new Share();
        share.setUsername(username);
        share.setFileId(file_id);
        share.setFileName(file_name);
        Date date = new Date();
        share.setCreateDate(date);
        Share existShare = shareService.getShareByUserFile(username,file_id);
        if (StringUtils.isEmpty(username) || file_id == 0) {
            res.put("url","链接生成出现问题，请重试");
        } else {
            if (existShare == null) {
                if (shareService.insertNewShare(share)) {
                    existShare = shareService.getShareByUserFile(username,file_id);
                    if (existShare != null) {
                        res.put("url","localhost:8081/#/share/"+existShare.getShareId());
                    }else {
                        res.put("url","链接生成出现问题，请重试");
                    }
                }else {
                    res.put("url","链接生成出现问题，请重试");
                }
            } else {
                res.put("url","localhost:8081/#/share/"+existShare.getShareId());
            }
        }
        return res;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 16:29 2018/6/11
     */
    @RequestMapping(value = "/getSharePage")
    @ResponseBody
    public Map<String,Object> getSharePage(@RequestBody Map<String,Object> params) {
        Map<String,Object> res = new HashMap<>();
        int share_id = Integer.parseInt( (String) params.get("share_id") );
        Share share = shareService.getShareById(share_id);
        if (share != null) {
            String fileName = share.getFileName();
            String username = share.getUsername();
            int fileId = share.getFileId();
            Date createDate = share.getCreateDate();
            DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String data = sdf.format(createDate);
            String content = filesService.getFileContent(username,fileId);
            res.put("file_name",fileName);
            res.put("username",username);
            res.put("content",content);
            res.put("create_data",data);
        } else {
            res.put("file_name","不正确的分享地址");
            res.put("username","未知");
            res.put("content","未知");
            res.put("create_date","未知");
        }
        return res;
    }

    /**
     * 举报
     * @Author: jy
     * @Description:
     * @Date: 16:34 2018/6/16
     */
    @RequestMapping(value = "/complaint")
    @ResponseBody
    public Map<String,Object> complaint(@RequestBody Map<String,Object> params) {
        Map<String,Object> res = new HashMap<>();
        int share_id = Integer.parseInt( (String) params.get("share_id") );
        Share share = new Share();
        share.setShareId(share_id);
        share.setComplaint(0);
        shareService.complaint(share);
        return res;
    }

    @RequestMapping(value = "/getComplaintsByPage")
    @ResponseBody
    public Map<String,Object> getComplaints(@RequestBody Map<String,String> params) {
        Map<String,Object> result = new HashMap<>();
        int pageNo = Integer.parseInt((String)params.get("pageNo"));
        int pageSize = Integer.parseInt((String)params.get("pageSize"));
        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        List<Share> shares = shareService.getComplaintsByPage(page);
        int total = shareService.getComplaintsCount();
        result.put("shares",shares);
        result.put("total",total);
        return result;
    }

    @RequestMapping(value = "/unComplaint")
    @ResponseBody
    public Map<String,Object> unComplaint(@RequestBody Map<String,Object> params) {
        Map<String,Object> res = new HashMap<>();
        int share_id = (Integer) params.get("shareId");
        Share share = new Share();
        share.setShareId(share_id);
        share.setComplaint(1);
        shareService.complaint(share);
        return res;
    }

    @RequestMapping(value = "/deleteShare")
    @ResponseBody
    public Map<String,Object> deleteShare(@RequestBody Map<String,Object> params) {
        Map<String,Object> res = new HashMap<>();
        int share_id = (Integer) params.get("shareId");
        shareService.deleteShare(share_id);
        res.put("code",1);
        return res;
    }

}
